const { Sequelize } = require('sequelize')
const { config } = require('../config')
const {
    DATABASE,
    DB_HOST,
    DB_PASSWORD,
    DB_PORT,
    DB_USER,
    DB_LANG
} = config.DB

// 初始化数据库
const sequelize = new Sequelize({
    database: DATABASE,
    username: DB_USER,
    password: DB_PASSWORD,
    host: DB_HOST,
    port: DB_PORT,
    dialect: DB_LANG,
    timezone: '+08:00',
    dialectOptions: {
        dateStrings: true,
        typeCast: true
    },
    define: {
        underscored: true,
        // timestamps: true,
        createdAt: 'created_at',
        updatedAt: 'updated_at'
    }
})

sequelize.sync({ force: true })

// 测试数据库连接是否成功
// try {
//     await sequelize.authenticate()

//     sequelize.sync({ force: false })

//     console.log('mysql数据库连接成功！')
// } catch (err) {
//     console.error('mysql数据库连接失败，', err)
// }

module.exports = sequelize